import { Pipe, PipeTransform } from "@angular/core";

@Pipe({ name: "storeName" })
export class StoreName implements PipeTransform {
    transform(store?) {
        if (!store || !store.id) return "";
        if (store.storeType == 'standalone') return store.merchant.storeName;
        else if (store.storeType == 'branch') return store.merchant.storeName
            + " " + store.branchName + "(NO." + store.branchNo + ")";
        else return store.merchant.storeName
            + " " + store.branchName;
    }
}

@Pipe({ name: "orderByParam" })
export class OrderByParam implements PipeTransform {
    transform(arr, param, isASC = true) {
        let result = [];
        if (Array.isArray(arr) && arr.length) {
            result = arr.sort((a, b) => {
                if (isASC) {
                    return a[param] - b[param];
                }
                return b[param] - a[param];
            });
        }
        return result;
    }
}

@Pipe({ name: "stringEllipsis" })
export class StringEllipsis implements PipeTransform {
    transform(str?, maxLength?) {
        maxLength = maxLength ? maxLength : 5;
        if (str && str.length && str.length > maxLength) {
            str = str.substring(0, maxLength) + "...";
        }
        return str;
    }
}

@Pipe({ name: "currencySymbol" })
export class CurrencySymbol implements PipeTransform {
    transform(price?) {
        let store = JSON.parse(localStorage.getItem("store"));
        let currencySymbol = store.setting.setup ? store.setting.setup.symbol : undefined;
        currencySymbol = currencySymbol ? currencySymbol : "¥";
        if (price || price == "0") {
            price = currencySymbol + "" + price;
        }
        return price;
    }
}

@Pipe({ name: "printModSupFor" })
export class PrintModSupFor implements PipeTransform {
    transform(item) {
        if (item.modifiers && item.modifiers.items.length) {
            return item.modifiers.items;
        } else {
            return [];
        }
    }
}

@Pipe({ name: "printComboSupFor" })
export class PrintComboSupFor implements PipeTransform {
    transform(item) {
        if (item.extended && item.extended.combo && item.extended.combo.group && item.extended.combo.group.length) {
            return item.extended.combo.group;
        } else {
            return [];
        }
    }
}

@Pipe({ name: "stringLimit" })
export class StringLimit implements PipeTransform {
    transform(str, length?) {
        let limit = length ? length : 5;
        if (str && typeof str == "string") {
            if (str.length < limit) {
                return str;
            } else {
                str = str.substring(0, limit);
                str += "...";
                return str;
            }
        }
        return "";
    }
}

@Pipe({ name: "waiterNames" })
export class WaiterNames implements PipeTransform {
    transform(table) {
        let names = ""
        if (table.tableParty && Array.isArray(table.tableParty.waiter) && table.tableParty.waiter.length) {
            table.tableParty.waiter.forEach((element, index) => {
                if (index == 0) {
                    names += element.name;
                } else {
                    names += ("," + element.name);
                }
            });
        } else if(table.masterOrder && table.masterOrder.creator){
            names = table.masterOrder.creator.name;
        }
        return names;
    }
}